package com.ass.page.web;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.ass.base.web.BaseController;
import com.ass.common.generated.dao.TArticleMapper;
import com.ass.common.generated.model.TArticle;
import com.ass.common.generated.model.TPermission;
import com.ass.common.service.CommonService;
import com.ass.common.utils.DateUtil;
import com.ass.common.utils.StringUtil;

@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {

	@Resource
	private CommonService commonService;
	
	@Resource
	private TArticleMapper tArticleMapper;
	
	
	//跳转到编辑页面
	@RequestMapping("/edit")
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mav = new ModelAndView("ass/article/updateArticle");
		String id = request.getParameter("id");
		if (StringUtil.isNotEmpty(id)) {
			mav.addObject("entity", tArticleMapper.selectByPrimaryKey(Long.parseLong(id)));
		}
		mav.addObject("statement", request.getParameter("statement"));
		mav.addObject("tourl", request.getParameter("tourl"));
		
		return mav;
		
	}
	
	//跳转到增加页面
	@RequestMapping("/toAdd")
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mav = new ModelAndView("ass/article/addArticle");
		mav.addObject("statement", request.getParameter("statement"));
		mav.addObject("tourl", request.getParameter("tourl"));
		return mav;
	}
	
	
	//保存
	@RequestMapping (value="/saveArticle")
	public ModelAndView saveMenu (HttpServletRequest request,TArticle art) {
		
		String editorValue = request.getParameter("editorValue");
		art.setContent(editorValue);
		art.setModifiedTime(DateUtil.getCurrentTimestamp());
		
		if(0 == art.getId()) tArticleMapper.insertSelective(art);
		else tArticleMapper.updateByPrimaryKeySelective(art);
		
		String tourl = request.getParameter("tourl");
		String statement = request.getParameter("statement");
		return commonService.search(tourl, statement);
	}
	
	
	@RequestMapping (value="/content")
	public void content (HttpServletRequest request,HttpServletResponse response) {
		
		String id = request.getParameter("id");
		TArticle t = null;
		if (StringUtil.isNotEmpty(id)) {
			t = tArticleMapper.selectByPrimaryKey(Long.parseLong(id));
			this.printStr(response, JSON.toJSONString(t));
			return ;
		}else
			this.printStr(response, JSON.toJSONString(t));
		
		
		
		
	}
	
	public static void main(String[] args) {
		TArticle t = null;
		System.out.println(JSON.toJSONString(t));
	}
	
	
	
	

}
